package com.library.controller;

import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.bean.Book;
import com.library.service.BookService;
import com.library.service.impl.BookServiceImpl;

@WebServlet("/updateBook.do")
public class UpdateBookServlet extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		BookService bookService = new BookServiceImpl();
		int bookId = Integer.parseInt(request.getParameter("bookId"));
		String bookName = request.getParameter("bookName");
		String bookType = request.getParameter("bookType");
		String bookWriter = request.getParameter("bookWriter");
		double bookPrice = Double.parseDouble(request.getParameter("bookPrice"));
		String pubDate = request.getParameter("pubDate");
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = dft.parse(pubDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Book book = new Book(bookName, bookType, bookWriter, bookPrice, date);
		book.setBookId(bookId);
		int result = bookService.updateBook(book);
		response.sendRedirect("showBook.do");

	}
	
}
